package WaitNotify;


public class TestDemo2 {
    public static void main(String[] args) {
        GuardedObject guardedObject = new GuardedObject();
        Thread t1 = new Thread(()->{
            System.out.println("计算中...");
            Integer x = guardedObject.get();
            int res = x + 89;
            System.out.println("结果为 :"+ res);
        });
        t1.start();
        Thread t2 = new Thread(()->{
            int a = 10 + 90;
            try {
                Thread.sleep(4000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            guardedObject.take(a);
        });
        t2.start();
    }
}
class GuardedObject {
    private Integer response;
    public Integer get(){
        while(response==null){
            synchronized (this){
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        return response;
    }

    public void take(Integer response){
        synchronized (this){
            this.response = response;
            this.notifyAll();
        }
    }
}